# -*- coding:utf-8 -*-
import os
import datetime

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker

from app.models.risk_manage import Risk_Application as RA
from config import DevelopmentConfig

"""
每天0点运行任务，计算风控的处于跟进状态的流程是否到期
"""
SQLALCHEMY_DATABASE_URI = DevelopmentConfig.SQLALCHEMY_DATABASE_URI

f = '%Y/%m/%d %H:%M:%S'

engine = create_engine(SQLALCHEMY_DATABASE_URI)

Session = sessionmaker(bind=engine)
session = Session()
ras = session.query(RA).all()


for ra in ras:
    if ra.deadline:
        if u'完结' not in ra.flow_status:
            dt = datetime.datetime.strptime(ra.deadline, f)
            now = datetime.datetime.now()
            a = dt - now
            if 0 <= a.days < 3:
                ra.loan_status = u'即将到期'
            elif a.days < 0:
                ra.loan_status = u'逾期'
                ra.flow_status = u'还款已完结'
                ra.loan_status = u'完结'
            session.add(ra)
            session.commit()
